<template>
    <view class="container paddingBottomBar">
        <header-bar :config="config"></header-bar>
        <uni-load-more status="loading" v-if="isLoading"></uni-load-more>
        <view v-else>
            <view class="pure-top"></view>
            <view class="infomain">
                <view class="detail-header">
                    <image v-if="data.State == 8" class="topimg" src="/static/img/tui1.png" mode="aspectFill"></image>
                    <image v-if="data.State == 0" class="topimg" src="/static/img/tui2.png" mode="aspectFill"></image>
                    <image v-if="data.State == 2" class="topimg" src="/static/img/tui3.png" mode="aspectFill"></image>
                    <image v-if="data.State == 7" class="topimg" src="/static/img/tui4.png" mode="aspectFill"></image>
                    <image v-if="data.State == 1" class="topimg" src="/static/img/tui5.png" mode="aspectFill"></image>
                    <image v-if="data.State == 3" class="topimg" src="/static/img/tui6.png" mode="aspectFill"></image>
                    <text>{{ getBtnText() }}</text>
                </view>
                <view class="detail">
                    <view class="refund_top">
                        <view class="docleft"></view>
                        <view class="docright"></view>
                        <view class="refund_top_price">
                            <view class="">退款金额：</view>
                            <view class="">¥{{ data.PriceSum }}</view>
                        </view>
                        <view class="refund_top_b">
                            <view>商品金额：¥{{ data.OrgPriceSum }}</view>
                            <view>审核金额：¥{{ data.PriceSum }}</view>
                        </view>
                    </view>
                    <view class="detail-item detail-info">
                        <view class="docleft"></view>
                        <view class="docright"></view>
                        <view class="item-itle info">
                            <text class="label-text">申请信息</text>
                        </view>
                        <order-info-item label="退货单号" :value="data.En"></order-info-item>
                        <order-info-item label="退货时间" :value="data.AddTime"></order-info-item>
                        <order-relation label="关联订单" :value="data.OrderSn"></order-relation>
                    </view>

                    <view class="detail-item detail-list" v-if="Details && Details.length > 0">
                        <view class="item-itle state">
                            <text class="label-text">退货商品</text>
                        </view>
                        <view class="order-item-main" @click="goGoodsList(Details, 1)">
                            <view class="order-item-imgs">
                                <view class="img-item" v-for="(item, index) in showImgs" :key="index">
                                    <image mode="aspectFill" :src="easyGetImgUrl(item)" class="order-item-img"></image>
                                    <view class="order-item-name" v-if="itemname">{{ itemname }}</view>
                                </view>
                                <uni-icons type="more" v-if="imgs.length > 3"></uni-icons>
                            </view>
                            <view class="right-box">
                                <view class="num-box">
                                    <text class="order-desc">共{{ Details.length }}种</text>
                                </view>
                                <uni-icons type="arrowright" size="20" customIcons="icon-black"></uni-icons>
                            </view>
                        </view>
                    </view>
                    <view class="rel detail-item detail-list" v-if="DetailExs && DetailExs.length > 0">
                        <view class="docleft"></view>
                        <view class="docright"></view>
                        <view class="item-itle state">
                            <text class="label-text">驳回商品</text>
                        </view>
                        <view class="order-item-main" @click="goGoodsList(DetailExs, 2)">
                            <view class="order-item-imgs">
                                <view class="img-item" v-for="(item, index) in showImgsExs" :key="index">
                                    <image mode="aspectFill" :src="easyGetImgUrl(item)" class="order-item-img"></image>
                                    <view class="order-item-name" v-if="itemnameExs">{{ itemnameExs }}</view>
                                </view>
                                <uni-icons type="more" v-if="imgsExs.length > 3"></uni-icons>
                            </view>
                            <view class="right-box">
                                <view class="num-box">
                                    <text class="order-desc">共{{ DetailExs.length }}种</text>
                                </view>
                                <uni-icons type="arrowright" size="20" customIcons="icon-black"></uni-icons>
                            </view>
                        </view>
                    </view>
                    <view
                        class="rel"
                        v-if="(data.State != 8 && data.State != 0) || data.Remark || (Message && data.State == 8)">
                        <view class="docleft"></view>
                        <view class="docright"></view>
                        <view class="detail-item detail-list" v-if="data.State != 8 && data.State != 0">
                            <view class="item-itle state">
                                <text class="label-text">退货信息</text>
                            </view>
                            <view class="card-group card-group-tui">
                                <card-group-cell
                                    label="退货配送方式"
                                    :isShowRight="false"
                                    :isDefalut="true"
                                    :value="data.PostTypeName"></card-group-cell>
                                <card-group-cell
                                    v-if="data.PostType == 0"
                                    label="快递公司"
                                    @click.native="selExpress"
                                    :isShowRight="false"
                                    :isDefalut="true"
                                    :value="type"></card-group-cell>
                                <view v-if="showAddress">
                                    <card-group-cell
                                        label="取件地址"
                                        v-if="isUseTms"
                                        @click.native="selectTakeAddress"
                                        ref="takeAddress"
                                        :value="takeAddress ? getTakeAddressText(takeAddress) : '请选择取件地址'"
                                        :isDefalut="true"
                                        :isShowRight="false"
                                        :className="'jend'"></card-group-cell>
                                    <card-group-cell
                                        label="快递单号"
                                        v-else
                                        ref="odd"
                                        placeholder="请输入快递单号"
                                        :value="odd"
                                        :sao="true"
                                        :isInputView="data.State == 1"
                                        :isShowRight="data.State != 1"
                                        @click.native="goEmsInfo()"></card-group-cell>
                                    <card-group-cell
                                        label="收件信息"
                                        :value="address.ContactName + '　' + address.ContactTel"
                                        :isDefalut="true"
                                        :isShowRight="false"></card-group-cell>
									<card-group-cell
										label="退货仓库"
										:value="address.AddressName"
										:isDefalut="true"
										:isShowRight="false"></card-group-cell>
                                    <card-group-cell
                                        label="退货地址"
                                        :value="getAddressText(address)"
                                        :isDefalut="true"
                                        :isShowRight="false"></card-group-cell>
                                </view>
                            </view>
                        </view>
                        <view
                            class="detail-item detail-reason"
                            v-if="data.Remark || (data.ImageArray && data.ImageArray.length > 0)">
                            <view class="item-itle state">
                                <text class="label-text">退货原因</text>
                            </view>
                            <view class="reason-content">
                                <text v-if="data.Remark">{{ data.Remark }}</text>
                                <view class="reason-img-box" v-if="data.ImageArray && data.ImageArray.length > 0">
                                    <image
                                        class="reason-img"
                                        v-for="(item, index) in data.ImageArray"
                                        :src="easyGetImgUrl(item)"
                                        @tap="previewImage(index)"></image>
                                </view>
                            </view>
                        </view>
                        <view class="detail-item detail-reason" v-if="Message && data.State == 8">
                            <view class="item-itle state">
                                <text class="label-text">驳回原因</text>
                            </view>
                            <view class="reason-content">
                                <text>{{ Message }}</text>
                            </view>
                        </view>
                    </view>
                </view>
            </view>
            <view class="bottom-btn" v-if="data.State == 0 || data.State == 1">
                <text class="btn btn-success-hollow" v-if="data.State == 0" @click="cancellation">退单作废</text>
                <text class="btn btn-success-hollow" v-if="data.State == 1" @click="submitExpressage">提交</text>
            </view>
        </view>
        <uni-popup type="bottom" ref="expressage" background-color="#fff" :safe-area="false">
            <view class="pop-pay-way">
                <view class="title"><text>请选择快递公司</text></view>
                <scroll-view scroll-y="true" class="pay-way-scroll">
                    <view class="pay-way">
                        <view class="pay-way-item" v-for="(item, index) in expressList" @click="select(item, index)">
                            <text>{{ item.Name }}</text>
                        </view>
                    </view>
                </scroll-view>
                <button class="btn" @click="cancel">取消</button>
            </view>
        </uni-popup>
        <uni-popup type="bottom" ref="address" background-color="#fff" :safe-area="false">
            <view class="pop-pay-way pop-address">
                <view class="title"><text>请选择收件信息</text></view>
                <scroll-view scroll-y="true" class="pay-way-scroll">
                    <view class="pay-way">
                        <view
                            class="pay-way-item"
                            v-for="(item, index) in addressList"
                            @click="selectAddress(item, index)">
                            <text>{{ item.ContactName + "　" + item.ContactTel }}</text>
                        </view>
                    </view>
                </scroll-view>
                <button class="btn" @click="addresscancel">取消</button>
            </view>
        </uni-popup>
        <uni-popup ref="delTip" type="dialog">
            <uni-popup-dialog
                type="text"
                title="温馨提示"
                content="确定要作废此订单吗?"
                :before-close="true"
                @close="close"
                @confirm="doDel"></uni-popup-dialog>
        </uni-popup>
    </view>
</template>

<script>
import { mapMutations } from "vuex"
import orderInfoItem from "@/pages_shop/components/order/order-info-item.vue"
import cardGroupCell from "@/components/common/cell/card-group-cell.vue"
import orderRelation from "@/components/order/order-relation.vue"
import { GetReturnAddressList } from "@/service/api/user.js"
import {
    GetRefundOrderExtInfo,
    CancelRefundOrder,
    SaveRefundOrderAddress,
    GetDeliveryReturnRecord,
} from "@/service/api/exChange.js"
import { GetExpressList } from "@/service/api/company.js"
export default {
    components: {
        orderInfoItem,
        cardGroupCell,
        orderRelation,
    },
    data() {
        return {
            config: {
                title: "退单详情",
                color: ["#ffffff"],
                centerSlot: true,
                barPlaceholder: true,
            },
            id: 0,
            type: "请选择快递公司",
            odd: "",
            data: {},
            isLoading: false,
            expressId: null,
            express: [],
            expressList: [],
            address: {},
            addressList: [],
            Message: "",
            showAddress: false,
            isUseTms: false,
            takeAddress: null,
            showImgs: [],
            showImgsExs: [],
            imgs: [],
            imgsExs: [],
            itemname: "", //单个商品显示名称
            itemnameExs: "", //单个商品显示名称
            Details: [],
            DetailExs: [],
        }
    },
    created() {
        let mainColor = this.easyGetColor("main")
        this.config.backgroundColor = [1, mainColor]
    },
    onLoad(e) {
        this.id = e.id
    },
    mounted() {
        this.getDetail()
        this.getAddress()
    },
    methods: {
        ...mapMutations(["UP_ORDER_VIEW"]),
        getAddressText(address) {
            let street = address.StreetName ? address.StreetName : ""
            let contactAddress = address.ContactAddress ? address.ContactAddress : ""
            let result = address.ProvinceName + address.CityName + address.AreaName + street + contactAddress
            return result
        },
        getBtnText() {
            //获取订单状态对应按钮文字
            let btnText = ""
            switch (this.data.State) {
                case 0:
                    btnText = "待审核"
                    break
                case 1:
                    btnText = "待退货"
                    break
                case 2:
                    btnText = "已取消"
                    break
                case 3:
                    btnText = "退货中"
                    break
                case 7:
                    btnText = "已退货"
                    break
                case 8:
                    btnText = "已驳回"
                    break
                default:
                    btnText = "待审核"
                    break
            }
            return btnText
        },
        selExpress() {
            if (this.data.State == 1) {
                this.$refs.expressage.open()
            }
        },
        select(item, index) {
            this.type = item.Name
            this.expressId = item.ID
            this.isUseTms = item.Code == "tms"
            //商家配送 不显示物流信息
            if (item.Code == "shangjia") {
                this.showAddress = false
            } else {
                this.showAddress = true
            }
            this.$forceUpdate()
            this.$refs.expressage.close()
        },
        cancel() {
            this.$refs.expressage.close()
        },
        prompt() {
            uni.showToast({
                title: "已通知商家尽快处理",
                icon: "none",
                duration: 2000,
            })
        },
        submitExpressage() {
            var that = this
            let param = {
                id: this.id,
                userId: this.data.UserId,
                postType: this.data.PostType,
            }
            if (this.data.PostType == 0) {
                if (!this.expressId || this.expressId == 0) {
                    uni.showToast({
                        title: "请选择快递公司",
                        icon: "none",
                        duration: 2000,
                    })
                    return
                }
				param.expressId = this.expressId
                if (this.showAddress && !this.isUseTms) {
                    if (this.$refs.odd.getValue().trim() == "") {
                        this.easyToast("请输入快递单号")
                        return
                    }
                    param.expressName = this.type
                    param.expressNo = this.$refs.odd.getValue()
                }
                if (this.isUseTms) {
                    if (!this.takeAddress) {
                        this.easyToast("请填写取件信息")
                        return
                    }
                    param.expressName = this.type
                    param.sendAddressId = this.takeAddress.ID
                    param.address = this.takeAddress
                }
            }

            if (this.address) {
                param.addressId = this.address.ID
            }
            SaveRefundOrderAddress(param).then((res) => {
                if (res.State == 1) {
                    uni.showToast({
                        title: "提交成功",
                        icon: "none",
                        duration: 2000,
                    })
                    that.upOrderView()
                    setTimeout(() => {
                        that.getDetail()
                    }, 2000)
                } else {
                    uni.showToast({
                        title: res.Msg,
                        icon: "none",
                        duration: 2000,
                    })
                }
            })
        },
        goGoodsList(datas, type) {
            let data = encodeURIComponent(JSON.stringify(datas))
            this.easyGoTo(
                "/pages_shop/packet/return/returnGoodsList?id=" + this.data.ID + "&type=" + type + "&data=" + data,
            )
        },
        //获取详情
        getDetail() {
            let param = {
                id: this.id,
            }
            this.isLoading = true
            this.isUseTms = false
            GetRefundOrderExtInfo(param).then((res) => {
                this.data = res.Data.Info
                this.Details = res.Data.Details
                this.DetailExs = res.Data.DetailExs
                if (res.Data.Address) {
                    if (res.Data.Address.ExpressNo) {
                        this.odd = res.Data.Address.ExpressNo
                    }
                    if (res.Data.Address.ExpressName) {
                        this.type = res.Data.Address.ExpressName
                        this.showAddress = true
                    } else {
                        this.type = "商家配送"
                        this.showAddress = false
                    }
                } else {
                    this.showAddress = false
                }

                if (res.Data.CheckLog && res.Data.CheckLog.Message) {
                    this.Message = res.Data.CheckLog.Message
                }
				this.address = res.Data.Warehouse;
                this.getImageList(res.Data.Details)
                this.isLoading = false
                this.getExpList()
                this.getImageList(res.Data.Details)
                this.getImageListExs(res.Data.DetailExs)
            })
        },
        getImageList(list) {
            let arr = []
            if (list) {
                list.forEach((e) => {
                    arr.push(e.ProductImage)
                })
            }
            if (list.length == 1) {
                this.itemname = list[0].ProductName
            }
            this.imgs = arr
            this.showImgs = arr.slice(0, 3)
        },
        getImageListExs(list) {
            let arr = []
            if (list) {
                list.forEach((e) => {
                    arr.push(e.ProductImage)
                })
            }
            if (list.length == 1) {
                this.itemnameExs = list[0].ProductName
            }
            this.imgsExs = arr
            this.showImgsExs = arr.slice(0, 3)
        },
        getAddress() {
            let param = {
                keyword: "",
            }
            let path = "?keyword=''"
            GetReturnAddressList().then((res) => {
                if (res.State == 1) {
                    this.addressList = res.Data
                }
            })
        },
        getExpList() {
            let param = {
                isRefund: true,
            }
            let path = "?isRefund=true"
            GetExpressList(param, path).then((res) => {
                this.express = res.Data
                res.Data.forEach((e) => {
                    this.expressList.push(e)
                })
            })
        },
        //退单作废
        cancellation() {
            this.$refs.delTip.open()
        },
        _cancelOrder() {
            var that = this
            let param = {
                id: this.id,
                userId: this.data.UserId,
            }

            CancelRefundOrder(param).then((res) => {
                if (res.State == 1) {
                    uni.showToast({
                        title: "作废成功",
                        icon: "none",
                        duration: 2000,
                    })
                    that.upOrderView()
                    setTimeout(() => {
                        that.getDetail()
                    }, 2000)
                } else {
                    uni.showToast({
                        title: res.Msg,
                        icon: "none",
                        duration: 2000,
                    })
                }
            })
        },
        upOrderView() {
            this.UP_ORDER_VIEW(new Date().getTime())
        },
        //预览图片
        previewImage(index) {
            let that = this
            var current = index
            uni.previewImage({
                current: current,
                urls: that.easyImgList(that.data.ImageArray.join(",")),
            })
        },
        close() {
            this.$refs.delTip.close()
        },
        doDel() {
            this.close()
            this._cancelOrder()
        },
        changeAddress() {
            this.$refs.address.open()
        },
        addresscancel() {
            this.$refs.address.close()
        },
        selectAddress(item, index) {
            this.address = item
            this.$refs.address.close()
        },
        selectTakeAddress() {
            if (this.data.State != 1) {
                return
            }
            let selectId = this.takeAddress ? this.takeAddress.ID : 0
            uni.navigateTo({
                url: "/pages/address/index?storeUserId=" + this.data.UserId + "&from=select&selectId=" + selectId,
                events: {
                    selectEvent: (data) => {
                        console.log(data)
                        this.takeAddress = data
                    },
                },
            })
        },
        getTakeAddressText(obj) {
            console.log(obj)
            return `${obj.ContactName}　${obj.ContactTel}\n${obj.Address}`
        },
        goEmsInfo() {
            let param = {
                orderId: this.Id,
            }
            let path = "?orderId=" + this.id
            GetDeliveryReturnRecord(param, path).then((res) => {
                if (res.State == 1) {
                    let list = res.Data.OutLogisticsList
                    if (list && list.length > 0) {
                        let item = list[0]
                        this.easyGoTo(
                            "/pages/order/logistics?data=" + encodeURIComponent(JSON.stringify(item)) + "&type=1",
                        )
                    }
                }
            })
        },
    },
}
</script>

<style lang="scss" scoped>
.container {
    display: flex;
    flex-direction: column;
}
.rel {
    position: relative;
    .docleft {
        left: -20rpx;
        top: -40rpx;
        bottom: auto;
    }
    .docright {
        right: -20rpx;
        top: -40rpx;
        bottom: auto;
    }
}
.pure-top {
    width: 100%;
    height: 206rpx;
    position: relative;
    z-index: 0;
    overflow: hidden;

    &:after {
        content: "";
        width: 120%;
        height: 206rpx;
        position: absolute;
        left: -10%; //椭圆左边隐藏10%，右边隐藏10%
        top: 0;
        border-radius: 0 0 50% 50%; //左上角，右上角，右下角，左下角
        background: $app-color-main;
    }
}
.infomain {
    margin: 0 24rpx;
    margin-top: -206rpx;
    position: relative;
    z-index: 10;
}
.bottom-btn {
    padding: 0 24rpx;
    .btn {
        height: 84rpx;
        border-radius: 44rpx;
        border: 2rpx solid $app-color-main;
        margin-top: 48rpx;
        font-size: 32rpx;
        color: $app-color-main;
    }
}
.detail-header {
    height: 108rpx;
    display: flex;
    align-items: center;
    justify-content: center;
    image {
        margin-right: 10rpx;
    }
    text {
        font-weight: 500;
        font-size: $font-size-36;
        color: $color-white;
    }
}
.detail {
    background: #ffffff;
    border-radius: 16rpx;
    .docleft {
        position: absolute;
        left: -20rpx;
        bottom: -20rpx;
        width: 40rpx;
        height: 40rpx;
        border-radius: 50%;
        background: $uni-bg-color-grey;
        z-index: 2;
    }
    .docright {
        position: absolute;
        right: -20rpx;
        bottom: -20rpx;
        width: 40rpx;
        height: 40rpx;
        border-radius: 50%;
        background: $uni-bg-color-grey;
        z-index: 2;
    }
    .refund_top {
        padding: 0 24rpx;
        position: relative;
        border-bottom: 1rpx dotted #ebebeb;
        .refund_top_price {
            display: flex;
            font-size: 36rpx;
            color: $app-color-main;
            font-weight: bold;
            padding: 54rpx 0rpx 32rpx 0rpx;
        }
        .refund_top_b {
            font-size: 28rpx;
            color: #666;
            display: flex;
            justify-content: space-between;
            align-items: center;
            padding-bottom: 44rpx;
        }
    }
    .detail-info {
        margin-top: 18rpx;
        padding-bottom: 32rpx;
        border-bottom: 1rpx dotted #ebebeb;
        position: relative;
    }
    .detail-item {
        margin-bottom: 20rpx;
    }

    .item-itle {
        padding: $app-page-padding;
        display: flex;
        flex-direction: row;
        margin-bottom: 10rpx;
        align-items: center;
        .label-text {
            font-weight: 700;
            display: flex;
            flex: 1;
            color: $color-gray-1;
            font-size: $font-size-28;
        }
        .value-text {
            font-weight: 100;
            font-size: $font-size-28;
            color: $app-color-text-main;
            color: #000000;
        }
    }
    .price {
        .label-text {
            color: $color-red;
        }
        .value-text {
            font-weight: 700;
            color: $color-red;
        }
    }
    .detail-reason {
        display: flex;
        flex-direction: column;
        .reason-content {
            margin: $app-page-padding;
            padding: $app-page-padding $app-page-padding 34rpx $app-page-padding;
            background-color: $color-gray-5;
            text {
                font-size: $font-size-24;
            }
        }
        .reason-img {
            width: 120rpx;
            height: 120rpx;
            margin-right: 24rpx;
            border-radius: 4rpx;
        }
        .reason-img-box {
            margin-top: 24rpx;
            display: flex;
            flex-direction: row;
        }
    }
}
.pay-way-scroll {
    max-height: 50vh;
}
.order-info-item .label {
    font-size: $font-size-28;
}
.pop-address {
    .pay-way-scroll {
        height: 340rpx;
        border-bottom: solid 20rpx $border;
    }
    .pay-way {
        border-bottom: 0;
    }
}
.topimg {
    width: 48rpx;
    height: 48rpx;
}
.order-item-main {
    position: relative;
    display: flex;
    flex: 1;
    flex-direction: row;
    padding: 0 24rpx 48rpx 24rpx;
    border-bottom: 1rpx dotted #ebebeb;
}

.order-item-imgs {
    display: flex;
    flex: 1;
    flex-direction: row;
    align-items: center;

    .img-item {
        position: relative;
        margin-right: 24rpx;
        display: flex;
        align-items: center;

        .order-item-name {
            flex: 1;
            font-size: 28rpx;
            color: $color-gray-1;
            display: flex;
            align-items: center;
            padding-left: 14rpx;
        }

        .order-item-img {
            display: flex;
            width: 120rpx;
            height: 120rpx;
            // border: 1px solid $app-color-main;
            border-radius: 8rpx;
        }

        .text-num {
            display: flex;
            justify-content: center;
            align-items: center;
            position: absolute;
            bottom: 0;
            left: 0;
            right: 0;
            height: 40rpx;
            border-bottom-left-radius: 10rpx;
            border-bottom-right-radius: 10rpx;
            color: $color-white;
            background: rgba($color-black, 0.3);
            font-size: $font-size-24;
        }
    }
}

.right-box {
    display: flex;
    flex-direction: row;
    align-items: center;

    .num-box {
        display: flex;
        flex-direction: column;
        margin-right: 16rpx;

        text {
            margin-bottom: 4rpx;

            &:last-child {
                margin-bottom: 0;
            }
        }
    }
}
.card-group-tui .card-group-cell {
    border: none;
    padding: 12rpx $app-page-padding 12rpx 0;
}
</style>
